#!/usr/bin/perl -w

####################################################
# Copyright (c) 2002-2007 BioWare Corp.
# http://www.bigdaddymonitor.org
#
# This file is part of the BigDaddy Monitoring
# program. It is free software distributed under
# the terms of the BSD License. It is ditributed
# in the hopes that it will be useful, but
# WITHOUT ANY WARRANTY; without event the implied
# warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
#
# The BSD License is available from:
# http://www.opensource.org/licenses/bsd-license.php
####################################################

#===================================================================================================
# BigDaddy History Table Dump
# Dumps the History table into SQL dump files.
# Dups 17.04.2006
#===================================================================================================

BEGIN
{
	push(@INC,'/bigdaddy')
}

use strict;
use Config::IniFiles;
use BioMain qw(:allfunctions);
use DBI;
use Time::Local;

my $cronID=169;


#===================================================================================================
#Before we do anything we are going to bring up the main ini file: bigdaddy.ini should be in the
#same dir as these executables.
my $bigdaddy_cfg= new Config::IniFiles( -file => "/bigdaddy/bigdaddy.ini" );

#Get the mailserver
my $mailServer = $bigdaddy_cfg->val('main', 'smtpserver');

if (!defined($mailServer) || $mailServer eq '')
{
	$mailServer = "localhost";
}

#Get the Global From Email
my $fromEmail = $bigdaddy_cfg->val('main', 'fromemail');

#Get the Global From Email
my $normalEmail = $bigdaddy_cfg->val('main', 'noncriticalrestart');

#This Process Name
my $historyDumpDir = $bigdaddy_cfg->val('main', 'historydumpdir');

require ("/bigdaddy/bigdaddy_common.pl");

#===================================================================================================
&cronstart($cronID);

#Now find the time at midnight
my $timestampMidnight= timelocal(0,0,0,(localtime)[3,4,5]);

#This is the dump threshold (current_time-7days)
my $dumpThreshold=$timestampMidnight-(86400*7);

my ($seconds, $minutes, $hours, $printDay, $printMonth, $printYear, $wday, $yday, $isdst)=localtime($dumpThreshold);
my $printDay_c=addzero($printDay);
my $printMonth_c=addzero($printMonth+1);
my $printYear_c=addzero($printYear+1900);
my $print_str=$printYear_c.$printMonth_c.$printDay_c;

#first let's dump the data
#verify the directory name

my $filename = $historyDumpDir."bigdaddyhistory.".$print_str.".dump";


#Database Information2
my %DBInfo=get_dbinfo("bigdaddystats");
my $dbName2=$DBInfo{'database'};
my $dbUser2=$DBInfo{'user'};
my $dbPasswd2=$DBInfo{'pass'};
my $dbHost2=$DBInfo{'host'};

my $dbb = callfor_db_handle("bigdaddystats", 0, 99);

my $sql = "DROP TABLE server_module_historytemp";
$dbb->do( $sql );

$sql = "CREATE TABLE server_module_historytemp LIKE server_module_history";
$dbb->do( $sql );

$sql = "INSERT INTO server_module_historytemp 
	SELECT * FROM server_module_history WHERE mon_lastreport<".$dumpThreshold;
$dbb->do( $sql );	

my $command="mysqldump -u$dbUser2 -p$dbPasswd2 -h$dbHost2 $dbName2 --no-create-info --quick -e server_module_historytemp > ".$filename;
#print $command."\n";
system($command);

my $nFilesize = -s $filename;

if ($nFilesize > 10000)
{
	
	$sql="DELETE FROM server_module_history WHERE mon_lastreport<".$dumpThreshold;
	print $sql."\n";
	$dbb->do($sql);
	
	&cronlog($cronID, "Bigdaddy history table dumped ".$filename." ".$nFilesize." bytes");
	#&send_mail($normalEmail, "Bigdaddy history table dumped\n".$filename."\n".$nFilesize." bytes", "BigDaddy History Dump Success", $fromEmail, $mailServer );
}
else
{
	&cronlog($cronID, "Could not dump bigdaddy history table, please check");
	&send_mail($normalEmail, "Could not dump bigdaddy history table, please check", "BigDaddy History Dump Failure", $fromEmail, $mailServer );
}

$sql = "DROP TABLE server_module_historytemp";
$dbb->do( $sql );

&cronend($cronID);
